package leetcode_1001_1100;

import java.util.HashMap;
import java.util.Map;

public class LeeCode_1010 {
    public static void main(String[] args) {
        System.out.println(numPairsDivisibleBy60(new int[]{30, 20, 150, 100, 40}));
        System.out.println(numPairsDivisibleBy60(new int[]{60,60,60}));
    }
    private static int numPairsDivisibleBy60(int[] time) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int t : time) {
            map.put(t, map.getOrDefault(t, 0) + 1);
        }
        long ans = 0;
        for (Integer t : map.keySet()) {
            int i = 1;
            int t2 = 1;
            Integer c = map.get(t);
            while (t2 <= 500){
                t2 = i * 60 - t;
               if (t2 > 0){
                    if (map.containsKey(t2)){
                        if (t == t2){
                            ans += (long) c * (c - 1);
                        }else {
                            ans += (long) map.get(t2) * c;
                        }
                    }
                }
                i++;
            }
        }
        return (int) (ans / 2);
    }
}
